<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="./src/vue.js"></script>
    <style>
      .test {
        width: 100px;
        height: 100px;
        background-color: yellow;
      }
    </style>
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <transition
        v-on:before-enter="beforeEnter"
        v-on:enter="enter"
        v-on:after-enter="afterEnter"
        v-on:before-leave="beforeLeave"
        v-on:leave="leave"
        v-on:after-leave="afterLeave"
      >
        <div class="test" v-show="show"></div>
      </transition>
      <button @click="show=!show">显示/隐藏</button>
    </div>
    <script>
      const vm = new Vue({
        el: "#app",
        data: {
          show: true,
        },
        methods: {
          beforeEnter: function (el) {
            console.log("进入前: ", el);
          },
          enter: function (el, done) {},
          afterEnter: function (el) {},
          beforeLeave: function (el) {
            console.log("离开前: ", el);
            el.style.opacity = 1;
          },
          leave: function (el, done) {
            // el.style.transition = "all 5s";
            function setOpaticy() {
              let newOpacity = el.style.opacity * 1 - 0.01;
              el.style.opacity = newOpacity;
              if (newOpacity <= 0) {
                done();
              } else {
                setTimeout(() => {
                  setOpaticy(); // 递归调用
                }, 50);
              }
            }
            setOpaticy();
          },
          afterLeave: function (el) {
            console.log("离开后: ", el);
            el.style.opacity = 0;
          },
        },
      });
    </script>
  </body>
</html>
